import { ProductStockFlows } from './productStockFlow';
let _ProductStockFlows = ProductStockFlows;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import ProductStockFlowMock from './mallm/product/productStockFlowMock';
 * ProductStockFlowMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/product/productStockFlow/listKey').reply(config => {
      let params = config.params;
      let mockProductStockFlows = _ProductStockFlows.filter(productStockFlow => {
      	if(params.productId||params.productId==''){ if( productStockFlow.productId.indexOf(params.productId)>=0) return true;}
      	if(params.increaseQuantity||params.increaseQuantity==''){ if( productStockFlow.increaseQuantity.indexOf(params.increaseQuantity)>=0) return true;}
      	if(params.reduceQuantity||params.reduceQuantity==''){ if( productStockFlow.reduceQuantity.indexOf(params.reduceQuantity)>=0) return true;}
      	if(params.remark||params.remark==''){ if( productStockFlow.remark.indexOf(params.remark)>=0) return true;}
      	if(params.beforeQuantity||params.beforeQuantity==''){ if( productStockFlow.beforeQuantity.indexOf(params.beforeQuantity)>=0) return true;}
      	if(params.userid||params.userid==''){ if( productStockFlow.userid.indexOf(params.userid)>=0) return true;}
      	if(params.operDate||params.operDate==''){ if( productStockFlow.operDate.indexOf(params.operDate)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( productStockFlow.deptid.indexOf(params.deptid)>=0) return true;}
      	if(params.id||params.id==''){ if( productStockFlow.id.indexOf(params.id)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockProductStockFlows
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/product/productStockFlow/list').reply(config => {
      let params = config.params;
      let mockProductStockFlows = _ProductStockFlows.filter(productStockFlow => {
      	var isTrue=false;
      	if(params.productId||params.productId==''){if(productStockFlow.productId.indexOf(params.productId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.increaseQuantity||params.increaseQuantity==''){if(productStockFlow.increaseQuantity.indexOf(params.increaseQuantity)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.reduceQuantity||params.reduceQuantity==''){if(productStockFlow.reduceQuantity.indexOf(params.reduceQuantity)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.remark||params.remark==''){if(productStockFlow.remark.indexOf(params.remark)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.beforeQuantity||params.beforeQuantity==''){if(productStockFlow.beforeQuantity.indexOf(params.beforeQuantity)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.userid||params.userid==''){if(productStockFlow.userid.indexOf(params.userid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.operDate||params.operDate==''){if(productStockFlow.operDate.indexOf(params.operDate)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(productStockFlow.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.id||params.id==''){if(productStockFlow.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockProductStockFlows
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockProductStockFlows.length;
      mockProductStockFlows = mockProductStockFlows.filter((productStockFlow, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockProductStockFlows,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/product/productStockFlow/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _ProductStockFlows = _ProductStockFlows.filter(productStockFlow => productStockFlow.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/product/productStockFlow/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _ProductStockFlows = _ProductStockFlows.filter(productStockFlow => !params.includes(productStockFlow.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/product/productStockFlow/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { productId,increaseQuantity,reduceQuantity,remark,beforeQuantity,userid,operDate,deptid,id } = params;
      _ProductStockFlows.some(productStockFlow => {
        if (productStockFlow.id === id) {
			productStockFlow.productId = productId;
			productStockFlow.increaseQuantity = increaseQuantity;
			productStockFlow.reduceQuantity = reduceQuantity;
			productStockFlow.remark = remark;
			productStockFlow.beforeQuantity = beforeQuantity;
			productStockFlow.userid = userid;
			productStockFlow.operDate = operDate;
			productStockFlow.deptid = deptid;
			productStockFlow.id = id
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/product/productStockFlow/add').reply(config => {
      var params = JSON.parse( config.data );
      let { productId,increaseQuantity,reduceQuantity,remark,beforeQuantity,userid,operDate,deptid,id } = params;
      _ProductStockFlows.push({
		productId:productId,
		increaseQuantity:increaseQuantity,
		reduceQuantity:reduceQuantity,
		remark:remark,
		beforeQuantity:beforeQuantity,
		userid:userid,
		operDate:operDate,
		deptid:deptid,
		id:id
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};